<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row>
        <el-col :span="4">
          <el-button type="primary" @click="addCateDialog()">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table class="treeTbale" show-index index-text="#"
                  :data="cateList" :columns="columns" :selection-type="false"
                  :expand-type="false"
                  border
      >
        <!-- 是否有效 -->
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===true" style="color: lightgreen"></i>
          <i class="el-icon-error" v-if="scope.row.cat_deleted===false" style="color: red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="warning" v-else-if="scope.row.cat_level===2">三级</el-tag>
        </template>

        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"
                     @click="removeUserById(scope.row.id)"></el-button>
        </template>
      </tree-table>
      <!--      <el-table :data="cateList" border stripe>
              <el-table-column type="index" label="序号"></el-table-column>
              <el-table-column label="分类名称" prop="cat_name"></el-table-column>
              <el-table-column label="是否有效" prop="cat_deleted"></el-table-column>
              <el-table-column label="排序" prop="cat_level"></el-table-column>
              <el-table-column label="操作" width="180px">
                <template slot-scope="scope">
                  <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
                  <el-button type="danger" icon="el-icon-delete" size="mini"
                             @click="removeUserById(scope.row.id)"></el-button>
                  <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
                    <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRoles(scope.row)"></el-button>
                  </el-tooltip>
                </template>
              </el-table-column>
            </el-table>-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%" @close="closeAddCateForm()"
    >
      <el-form ref="addCateFormRef" :model="addCateForm" label-width="80px" :rules="addCateRules" >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
<!--        <el-form-item label="分类级别">-->
<!--          <el-input v-model="addCateForm.cat_level"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="父级级别">
          <el-cascader
            expand-trigger="hover"
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
            filterable
            style="width: 100%"
          >
          </el-cascader>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="saveCate()">确 定</el-button>
  </span>
    </el-dialog>
  </div>


</template>

<script>
  export default {
    created () {
      return this.getCateList()
    },
    data () {
      return {
        cateList: [],
        queryInfo: {
          pagenum: 1,
          pagesize: 10,
          type: 3

        },
        total: 0,
        addCateDialogVisible: false,
        addCateForm: {
          cat_name: '',
          pid: 0,
          cat_level: 0

        },
        parentCateList: [],
        cascaderProps: {
          label: 'cat_name',
          value: 'cat_id',
          children: 'children',

        },
        selectedKeys: [],
        addCateRules: {
          cat_name: [{
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          },
            {
              min: 3,
              max: 5,
              message: '长度在 3 到 5 个字符',
              trigger: 'blur'
            }]
        },
        columns: [
          {
            label: '分类名称',
            prop: 'cat_name'
          }, {
            label: '是否有效',
            prop: 'cat_deleted',
            //表示当前使用自定义模板
            type: 'template',
            //模板名称
            template: 'isOk'
          }, {
            label: '排序',
            prop: 'cat_level',
            type: 'template',
            //模板名称
            template: 'order'
          }, {
            label: '操作',
            type: 'template',
            //模板名称
            template: 'opt'
          }
        ]
      }
    },
    methods: {
      //建通pagesize
      handleSizeChange (newSize) {
        this.queryInfo.pagesize = newSize
        this.getCateList()
      },
      //监听pagenum
      handleCurrentChange (newPagenum) {
        this.queryInfo.pagenum = newPagenum
        this.getCateList()
      },
      async getCateList () {
        const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.cateList = res.data.result
        this.total = res.data.total
      },
      addCateDialog () {
        this.addCateDialogVisible = true
        this.getParentCateList()

      },
      async getParentCateList () {
        const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.parentCateList = res.data
      },
      parentCateChange () {
        let length = this.selectedKeys.length
        if (length === 2) {
          this.addCateForm.cat_pid = this.selectedKeys[length - 1]
          this.addCateForm.cat_level = length
        } else {
          this.addCateForm.cat_pid = 0
          this.addCateForm.cat_level = 0
        }
      },
      saveCate () {
        this.$refs.addCateFormRef.validate(async valid => {
          if (!valid) {
            return
          } else {
            const { data: res } = await this.$http.post('categories', this.addCateForm)
            if (res.meta.status !== 201) {
              return this.$message.error(res.meta.msg)
            } else {
              this.$message.success(res.meta.msg)
              this.getCateList()
              this.addCateDialogVisible = false
            }
          }
        })
      },
      async closeAddCateForm () {
        this.$refs.addCateFormRef.resetFields()
        this.addCateForm = {}
        this.selectedKeys = []
        // this.addCateDialogVisible = false
      }

    }
  }
</script>

<style lang="less" scoped>
  .el-cascader {
    width: 100%;
  }

  .treeTbale {
    margin-top: 15px;
  }


</style>
